package tutorial.main.data;


import java.util.ArrayList;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;
import tutorial.entidades.Evento;

public class ProviderRegistro extends ContentProvider {

	public static final Uri CONTENT_URI = Uri
			.parse("content://tutorial.providerregistro");
	//public static final String COD_USUARIO = "cod_usuario";
	public static final String CONSULTA_EVENTOS = "cons_eventos";
	public static final String CONSULTA_ULTIMO_NMRO = "cons_ultimo_nmro";
	public static final String CONSULTA_ULTIMO_EVENTO = "cons_ultimo_evento";
	public static final String UPDATE_EVENTO="update_evento";
	public static final int EXITO=1;
	public static final int FALLO=-1;
	/*public static final String CONSULTA_NOMBRE_COLUMNA = "nombreCol";
	public static final String COLUMN_ESTADO_CONECTADO = "estado_conectado";
	public static final String COLUMN_SINCRONIZADO = "sincronizado";
	public static final String COLUMN_MOMENTO = "momento";
	public static final String COLUMN_TIPO_CONEXION = "tipo_conexion";
	public static final String COLUMN_SUBTIPO_CONEXION = "subtipo_conexion";
	public static final String COLUMN_REASON = "reason";
	public static final String COLUMN_COD_USUARIO = "cod_usuario";
	public static final String COLUMN_ID = android.provider.BaseColumns._ID;*/
	public RegistroDataBaseHelper dBHelper;
	
	/*public static final String COUNTRIES_OF_CONTINENT = "countriesOfContinent";
	public static final String COUNTRY_OF_CONTINENT = "countryOfContinent";
	public static final String UPDATE_COUNTRY = "updateCountry";*/

	@Override	
	public int delete(Uri uri, String selection, String[] selectionArgs) 
	{
	 
	    int cont;	 
	   
	 
	    cont = dBHelper.eliminarEvento(selectionArgs);
	 
	    return cont;
	}

	@Override
	public String getType(Uri uri) {
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		
		dBHelper.insertarEventos(values);
		
		return null;
	}

	@Override
	public boolean onCreate() {
		dBHelper = new RegistroDataBaseHelper(getContext());
		return dBHelper != null;
	}

	
	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		
		Cursor cursor = null;
		if (CONSULTA_EVENTOS.equals(selection)) {
			if (selectionArgs.length == 1 && selectionArgs[0] != null) {
				cursor = dBHelper.getEventosDeUsuario(selectionArgs[0]);
			}
		} 
		else if (CONSULTA_ULTIMO_NMRO.equals(selection)) 
		{
				cursor = dBHelper.getUltimoNmro();
		}
		else if (CONSULTA_ULTIMO_EVENTO.equals(selection)) 
		{
				cursor = dBHelper.getUltimoEvento();
			
		}
		
		

		return cursor;
	}
	
	
	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) 
	{		
		

		if (UPDATE_EVENTO.equals(selection)) {
			
			// if (selectionArgs.length > 1 && selectionArgs[0] != null) // Estaba esto, pero nunca entra
			if (selectionArgs.length == 1 && selectionArgs[0] != null) 
			{
				String[] idsEvents={String.valueOf(selectionArgs[0])};
				long rows =  dBHelper.actualizarEventos(idsEvents, values);
				if(rows>0)
				{return EXITO;}
			}
		}
		
		return FALLO;
	}
	
	
}
